package com.filldream.fastboot.common.constant.enums;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;
import java.util.Arrays;

@Getter
public enum StockFstEnum {

    min(1,"quotation_minute_", null,"1")
    ,five(2,"quotation_fiveday_", null,"5")
    ,dayK(3,"quotation_kline_", "day","101")
    ,weekK(4,"quotation_kline_", "week","102")
    ,monthK(5,"quotation_kline_", "month","103")
    ,min1K(6,"quotation_kline_", "min1","1")
    ,min5K(7,"quotation_kline_", "min5","5")
    ,min15K(8,"quotation_kline_", "min15","15")
    ,min30K(9,"quotation_kline_", "min30","30")
    ,min60K(10,"quotation_kline_", "min60","60")
    ,quarterK(11,"quotation_kline_", "quarter","104")
    , halfYearK(13,"quotation_kline_", "quarter","105")
    ,yearK(12,"quotation_kline_", "year","106")
    ;

    private Integer type;

    private String group;

    private String kType;

    private String cfKType;

    //公共参数及地址
    private final static String baseUrl = "https://finance.pae.baidu.com/vapi/v1/getquotation?srcid=5353&pointType=string&newFormat=1&is_kc=0";
    //模板地址
    private final static String templateUrl = baseUrl +"&group=%group&query=%code&code=%code&market_type=%marketType&ktype=%ktype";

    StockFstEnum(Integer type, String group, String kType,String cfKType) {
        this.type = type;
        this.group = group;
        this.kType = kType;
        this.cfKType = cfKType;
    }

    /**
     * 获取真实请求地址
     * @param type 1、分时 2、五日 3、日K 4、周K 5、月K 6、1分 7、5分 8、15分 9、30分 10、60分 11、季K 12、年K
     * @param searchCode 股票代码
     * 参考:https://finance.pae.baidu.com/vapi/v1/getquotation?srcid=5353&pointType=string&group=quotation_minute_hk&query=00700&code=00700&market_type=hk&newFormat=1&name=%E8%85%BE%E8%AE%AF%E6%8E%A7%E8%82%A1&is_kc=0&newFormat=1
     * @return
     */
    public static String getRealUrl(Integer type,String searchCode){
        String marketType = searchCode.substring(0,2);
        marketType = marketType.equals("sh") || marketType.equals("sz")? "ab" : marketType;
        String stockCode = searchCode.substring(2);
        String url = templateUrl;
        url = url.replace("%code",stockCode);
        url = url.replace("%marketType",marketType);
        StockFstEnum stockFstEnum = Arrays.stream(StockFstEnum.values()).filter(t -> t.getType().equals(type)).findFirst().orElse(null);
        url = url.replace("%group",stockFstEnum.getGroup() + marketType);
        if( stockFstEnum.getKType() != null ){
            url = url.replace("%ktype",stockFstEnum.getKType());
        }else{
            url = url.replace("&ktype=%ktype","");
        }
        return url;
    }

    /**
     * 获取K线图数据
     * @param type   1、分时 2、五日 3、日K 4、周K 5、月K 6、1分 7、5分 8、15分 9、30分 10、60分 11、季K 12、年K
     * @param searchCode 股票代码
     * @return
     */
    public static JSONObject getKLine(Integer type, String searchCode ){
        String url = StockFstEnum.getRealUrl(type, searchCode);
        String json = HttpUtil.get(url);
        JSONObject jsonObject = JSONObject.parseObject(json);
        if( jsonObject.containsKey("ResultCode") && jsonObject.getIntValue("ResultCode") == 0
            && jsonObject.getJSONObject("Result") != null
        ){
            JSONObject marketData = jsonObject.getJSONObject("Result").getJSONObject("newMarketData");
            if(  type.equals( StockFstEnum.min.getType() ) ){
                JSONArray jsonArray = jsonObject.getJSONObject("Result").getJSONArray("buyinfos");
                marketData.put("buyinfos",jsonArray);
            }
            return marketData;
        }
        return null;
    }

    public static StockFstEnum getByType(Integer type){
        StockFstEnum stockFstEnum = Arrays.stream(StockFstEnum.values()).filter(t -> t.getType().equals(type)).findFirst().orElse(null);
        return stockFstEnum;
    }

}
